package com.tc.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tc.dto.BackdownDTO;
import com.tc.domain.Backdown;
import com.tc.domain.Bed;
import com.tc.domain.Customer;
import com.tc.domain.Outward;
import com.tc.service.BackdownService;
import com.tc.service.BedService;
import com.tc.service.CustomerService;
import com.tc.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/backdown")
@Api(tags = "退住管理")
@CrossOrigin
public class BackdownController {

    @Autowired
    private BackdownService backdownService;

    @Autowired
    private BedService bedService;

    @Autowired
    private CustomerService customerService;

    // 函数注释 行间注释 添加日志 生成单测 代码解释 接口文档 调优建议
    // private
    @PostMapping("/listBackdown")
    @ApiOperation("查询退住信息")
    public ResultVo listBackdown(BackdownDTO backdownDTO) throws Exception {
        return backdownService.listBackdownVo(backdownDTO);
    }

    // 函数注释 行间注释 添加日志 生成单测 代码解释 接口文档 调优建议
    @ApiOperation("添加退住审批")
    @PostMapping("/addBackdown")
    public ResultVo addBackdown(Backdown backdown) throws Exception {
        backdownService.save(backdown);
        return ResultVo.ok("添加成功");
    }
    @PostMapping("/examineBackdown")
    @ApiOperation("审批退住")
    public ResultVo examineBackdown(Backdown backdown) throws Exception {;
        Backdown bd = backdownService.getById(backdown.getId());
        // 审批通过
        if (backdown.getAuditStatus() == 1) {
            // 修改床铺记录，对应床铺状态改为空闲
            Customer cs = customerService.getById(bd.getCustomerId());
            Bed bed = new Bed();
            bed.setId(cs.getBedId());
            bed.setBedStatus(1);
            bedService.updateById(bed);
        }
        return backdownService.examineBackdown(backdown);
    }
    @PostMapping("/delBackdown")
    @ApiOperation("撤回退住申请")
    public ResultVo delBackdown(Integer id) throws Exception {
        UpdateWrapper<Backdown> updateWrapper = new UpdateWrapper<Backdown>();
        updateWrapper.eq("id", id);
        updateWrapper.set("is_deleted", 1);
        backdownService.update(updateWrapper);
        return ResultVo.ok("撤回成功");
    }
}
